﻿/*
	SimpleSlider
	
	*** Updated 3.13.2005 to extend mx.core.View instead of mx.core.UIComponent
	
	Probably the worst class I've ever coded.
	If you have a better slider, please feel
	free to replace this hunk-a-junk.
	
	Really, the main problem is that the
	updating of the slider's position
	is not really accurate to the actual
	sliderValue.
	
	Created by Jesse R. Warden a.k.a. "JesterXL"
	jesterxl@jessewarden.com
	http://www.jessewarden.com
	11.19.2004
	
	This is release under a Creative Commons license. 
    More information can be found here:
    
    http://creativecommons.org/licenses/by/2.5/
*/

import mx.core.View;
import mx.utils.Delegate;

class com.jxl.controls.SimpleSlider extends View
{
	static var symbolName:String = "com.jxl.controls.SimpleSlider";
	static var symbolOwner:Object = com.jxl.controls.SimpleSlider;
	public var className:String = "SimpleSlider";
	
	private var _sliderValue:Number = 50;
	
	public function get sliderValue():Number
	{
		return _sliderValue;
	}
	
	public function set sliderValue(val:Number):Void
	{
		setSliderValue(val);
		doLayout();
	}
	
	private var slider_mc:MovieClip;
	private var bar_mc:MovieClip;
	private var deadPreview_mc:MovieClip;
	
	function SimpleSlider()
	{
	}
	
	public function init():Void
	{
		super.init();
		
		this.tabEnabled = false;
		this.tabChildren = false;
	}
	
	private function createChildren(Void):Void
	{
		createChild("SimpleSliderBarSymbol", "bar_mc");
		bar_mc.onPress = Delegate.create(this, doBarPress);
		//bar_mc.tabEnabled = false;
		//bar_mc.tabChildren = false;
		
		createChild("SimpleSliderSliderSymbol", "slider_mc");
		slider_mc.onPress = Delegate.create(this, doSliderPress);
		slider_mc.onRelease = Delegate.create(this, doSliderRelease);
		slider_mc.onReleaseOutside = Delegate.create(this, doSliderRelease);
		//slider_mc.onSetFocus = Delegate.create(this, onSliderSetFocus);
		//slider_mc.onKillFocus = Delegate.create(this, onSliderKillFocus);
		//slider_mc.tabEnabled = true;
		//slider_mc.tabChildren = false;
		//slider_mc.tabIndex = 0;
	}
	
	private function doLayout(Void):Void
	{
		super.doLayout();
		bar_mc._width = width;
		bar_mc._height = height;
		slider_mc._height = height;
	}
	/*
	private function onSliderSetFocus():Void
	{
		var c:Color = new Color(slider_mc.outline_mc);
		var theColor = (origBorderStyles.themeColor == null) ? 0xFFFF00 : origBorderStyles.themeColor;
		c.setRGB(theColor);
	}
	
	private function onSliderKillFocus():Void
	{
		var c:Color = new Color(slider_mc.outline_mc);
		c.setRGB(0x666666);
	}
	*/
	private function setSliderValue(val:Number):Void
	{
		_sliderValue = val;
		doLater(this, "updateSliderPos");
		dispatchEvent({type: "change", target: this});
	}
	
	public function setSliderValueNoUpdate(val:Number):Void
	{
		_sliderValue = val;
		dispatchEvent({type: "change", target: this,
					  sliderValue: sliderValue});
	}
	
	public function updateSliderPos():Void
	{
		var x = Math.floor(bar_mc._width * (sliderValue / 100));
		slider_mc._x = x;
	}
	
	function doBarPress():Void
	{
		var w = bar_mc._width;
		var p = Math.round((bar_mc._xmouse / w) * 100);
		var a = bar_mc._width - slider_mc._width;
		var b = bar_mc._width * (p / 100)
		slider_mc._x = Math.min(a, b);
		setSliderValueNoUpdate(p);
	}
	
	function doSliderPress(Void):Void
	{
		slider_mc.myBounds = bar_mc.getBounds(this);
		slider_mc.total = (slider_mc.myBounds.xMax - slider_mc._width) - slider_mc.myBounds.xMin + 1;
		slider_mc.startDrag(false, 
					   slider_mc.myBounds.xMin, 
					   slider_mc.myBounds.yMin,
					   slider_mc.myBounds.xMax - slider_mc._width,
					   slider_mc.myBounds.yMin);
		slider_mc.onMouseMove = Delegate.create(this, sliderMouseMove);
	}
	
	function sliderMouseMove():Void
	{
		var pos:Number = (slider_mc._x - slider_mc.myBounds.xMin) + 1;
		slider_mc.percent = Math.round((pos / slider_mc.total) * 100);
		if(slider_mc.percent != 1)
		{
			setSliderValueNoUpdate(slider_mc.percent);
		}
		else
		{
			setSliderValueNoUpdate(0);
		}
		updateAfterEvent();
	}
	
	function doSliderRelease(Void):Void
	{
		delete slider_mc.onMouseMove;
		slider_mc.stopDrag();
	}
}